package com.example.appointment.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import java.util.ArrayList;

/**
 * SharedPreferences存储数据方式工具类
 */
public class SharedPrefsUtil
{
    public final static String SETTING = "Setting";

    private final static String Splitter = "%Splitter%";

    public static void
    putValue(Context context, String key, int value)
    {
        Editor sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE).edit();
        sp.putInt(key, value);
        sp.commit();
    }

    public static void putValue(Context context, String key, boolean value)
    {
        Editor sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE).edit();
        sp.putBoolean(key, value);
        sp.commit();
    }

    public static void putValue(Context context, String key, String value)
    {
        Editor sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE).edit();
        sp.putString(key, value);
        sp.commit();
    }

    public static void putValue(Context context, String key, ArrayList<String> stringArrayList)
    {
        Editor sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE).edit();
        String value = "";
        for (int i = 0; i < stringArrayList.size(); i++)
        {
            value = value + Splitter + stringArrayList.get(i);
        }
        value = value.replaceFirst(Splitter, "");
        sp.putString(key, value);
        sp.commit();
    }

    public static int getValue(Context context, String key, int defValue)
    {
        SharedPreferences sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE);
        int value = sp.getInt(key, defValue);
        return value;
    }

    public static boolean getValue(Context context, String key, boolean defValue)
    {
        SharedPreferences sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE);
        boolean value = sp.getBoolean(key, defValue);
        return value;
    }

    public static String getValue(Context context, String key, String defValue)
    {
        SharedPreferences sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE);
        String value = sp.getString(key, defValue);
        return value;
    }

    public static ArrayList<String> getValue(Context context, String key)
    {
        SharedPreferences sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE);
        String value = sp.getString(key, "");

        if (value.isEmpty())
            return new ArrayList<String>();

        String[] valueArray = value.split(Splitter);
        ArrayList<String> stringArrayList = new ArrayList<String>();

        for (int i = 0; i < valueArray.length; i++)
            stringArrayList.add(valueArray[i]);

        return stringArrayList;
    }

    public static void removeValue(Context context, String key)
    {
        Editor sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE).edit();

        sp.remove(key);
        sp.commit();
    }

    public static boolean isArrayListContainItem(Context context, String arraykey, String itemId)
    {

        boolean bContains = false;

        ArrayList<String> qiKanArrayList = SharedPrefsUtil.getValue(context, arraykey);

        if (qiKanArrayList != null)
        {
            bContains = CmnObjectFuncs.isArrayListContainsString(qiKanArrayList, itemId);

        }
        return bContains;

    }

}

